/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conexion;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author Administrador
 */
public class SocketClient {
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private SocketListener listener;
    private CommunicationThread comm;

    public SocketClient(String host, int port,SocketListener listener){
        this.listener = listener;
        listenSocket(host, port);
    }
    public void listenSocket(String host,int port){
    //Create socket connection
       try{
         socket = new Socket(host, port);
         out = new PrintWriter(socket.getOutputStream(), 
                     true);
         in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
       } catch (UnknownHostException e) {
         System.out.println("Unknown host: kq6py");
         System.exit(1);
       } catch  (IOException e) {
         System.out.println("No I/O");
         System.exit(1);
       }
       comm = new CommunicationThread(in, listener);
       comm.start();
    }
    public void send(String data){
        System.out.println("enviando : "+data);
        out.println(data);
    }
}
